(function($, document, window) {
  'use strict';

  var ns = jfJQMAmobeeAds;
  
  function onPageShowLoadAds (e) {
    var $page = $(e.target);
    $(function() { //wait for domReady Event if this is the first call
      ns.sendAdServerRequest($page);
    });
  }
  
  function onPageShowReset (e) {
    var $page = $(e.target);
    ns.resetPage($page);
  }

  var nsSendAdServerRequest = ns.sendAdServerRequest;
  ns.sendAdServerRequest = function($page, ads, responsemarkup) {
    $page = $page || $.mobile.activePage;
    nsSendAdServerRequest($page, ads, responsemarkup);
  };

  if (ns.getConfig('autoLoadAds', true)) {
    ns.setConfig('autoLoadAds', false);
    $("div:jqmData(role='page')").live('pageshow', onPageShowLoadAds);
  }
  
  $("div:jqmData(role='page')").live('pageshow', onPageShowReset);
  
})(jQuery, document, window);